/**
* ...
* @author Copyright 2007, Will Schenk, wschenk@gmail.com http://sublimeguile.com
* @licence  http://creativecommons.org/licenses/by-nc-sa/2.0/
* @Pv3D_version Exey Panteleev
* 
*/
package {
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.DisplayObject3D;

public class Plant extends DisplayObject3D {
	
	private var numIterations:int;
	
	public var len:Number;
	public var branches:Array;
	public var lens:Array;
	public var rootOfPlant:GrowingArm;
	
	public var lineMat:LineMaterial;
	public var containerForDrawing:Lines3D; 
	
	public function Plant(len:int, numIterations:int, plantType:uint = 2) {
		this.numIterations = numIterations;
		this.len = len;		  
		createPlant(plantType);
	}
	
	public function createPlant(plantType:uint):void {
		//PV3D
		lineMat = new LineMaterial(0xffffff);
		containerForDrawing = new Lines3D(lineMat); 
		addChild(containerForDrawing);
		
		//ITERATORS
		if (plantType == 1){
			rootOfPlant= new GrowingArm( len/6, radians( 90 ), null, containerForDrawing, lineMat );
			rootOfPlant.hindge_angle = randRange( radians( 0 ), radians( 180 ) );
			rootOfPlant.len = 1;

			for( var i:int = -3; i < 3; i++ ) {
			  var b:GrowingArm = rootOfPlant.add_child( len/10, i * (Math.PI/10), containerForDrawing, lineMat );

			  for( var k:int = -2; k < 2; k++ ) {
				var c:GrowingArm = b.add_child( len/10, k * (Math.PI/10), containerForDrawing, lineMat );

				for( var j:int = 0; j < 10; j++ ) {
				  //c = c.add_child( len/16, randRange( -Math.PI/16, Math.PI/16 ), containerForDrawing, lineMat );
				  c = c.add_child( len/16, randRange( -Math.PI/16, Math.PI/16 ), containerForDrawing, lineMat );
				}
			  }
			}
		}else if(plantType == 2)  {
			rootOfPlant = new GrowingArm( len/5, radians( 90 ), null, containerForDrawing, lineMat );
			rootOfPlant.len = 1;
			//trace("Plant2", rootOfPlant)
			
			  branches = new Array(3);
			  lens = new Array(3);	
			  
			  for( var i:int = 0; i < 3; i++ ) {
				branches[i] = randRange( -Math.PI/6, Math.PI/6 );
				lens[i] = randRange( -.2, .99 );
			  
			  create_branches( numIterations, len/5, rootOfPlant);
			}			
		}
	}
	
	public function draw(x:int, y:int, z:int) {
		//trace("Plant:draw", x , y, z)
		if( rootOfPlant ) {
		 rootOfPlant.draw( Math.PI, x, y, z, 0 );
		  rootOfPlant.wobble();
		}
		//trace(containerForDrawing.lines.length)
		//trace(branches.length, lens.length);
	}
	
	private function create_branches(level:int, len:Number, base:GrowingArm):void {
		level--;
		  
		for( var i:int = 0; i < branches.length; i++ ) {
		  var branch_length:int = int((len * lens[i]));
		  
		  var branch:GrowingArm = base.add_child( branch_length, branches[i], containerForDrawing, lineMat);
		  if( level > 0 )
			create_branches( level, branch_length, branch);
		}	
	}
	
	/****
	 * functions for easy port from processing
	 ****/
	private function radians(degrees:Number):Number {
		return degrees * Math.PI / 180;
	}
	private function degrees(radians:Number):Number {
		return radians * 180 / Math.PI;
	}
	private function randRange(min:Number, max:Number):Number {
		var randomNum:Number = (Math.random() * (max - min + 1)) + min;
		return randomNum;
	}//randRange without floor
}
}